//
//  OnlineExamViewController.m
//  jinnengExam
//
//  Created by mac on 2020/11/13.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "EOnlineTrainCourselistViewController.h"
#import "TrainFileListViewController.h"
#import "EOnlineTrainDetailViewController.h"
#import "ExamConfirmViewController.h"
#import "OnlineExamResultListViewController.h"
#import "OnlineExamViewController.h"
#import "ExamConfirmTestViewController.h"

@interface EOnlineTrainCourselistViewController ()
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@end

@implementation EOnlineTrainCourselistViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
    self.tabBarController.tabBar.hidden = YES;
    [self initView];
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.bottom.equalTo(self.view);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/onlineTrainCourseList" type:@"html"];

    
    [self.jznWebView.scrollView.mj_header setBackgroundColor:[UIColor whiteColor]];
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestLearn:self.page];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
    [self registJSFunction];
}

-(void) registJSFunction{
    [self.webViewBridge registerHandler:@"toFileList" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *fileList = data[@"fileList"];
        dispatch_async(dispatch_get_main_queue(), ^(){
            TrainFileListViewController *vc = [[TrainFileListViewController alloc]init];
            vc.fileListString = fileList;
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toOnlineExamComfirm" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *exam = data[@"exam"];
        dispatch_async(dispatch_get_main_queue(), ^(){
            ExamConfirmViewController *vc = [[ExamConfirmViewController alloc]init];
            [vc setExamJson:exam];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toResultList" handler:^(id data, WVJBResponseCallback responseCallback) {
        int onlineExamUserId = [data[@"onlineExamUserId"] intValue];
        dispatch_async(dispatch_get_main_queue(), ^(){
            OnlineExamResultListViewController *vc = [[OnlineExamResultListViewController alloc]init];
            [vc setOnlineExamUserId:onlineExamUserId];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toShowError" handler:^(id data, WVJBResponseCallback responseCallback) {
         //NSString *exam = data[@"exam"];
         dispatch_async(dispatch_get_main_queue(), ^(){
             [[UIApplication sharedApplication].delegate.window makeToast:@"有未达标的课程，请先完成学习"];
         });
     }];
    
    [self.webViewBridge registerHandler:@"toShowErrorTest" handler:^(id data, WVJBResponseCallback responseCallback) {
         //NSString *exam = data[@"exam"];
         dispatch_async(dispatch_get_main_queue(), ^(){
             [[UIApplication sharedApplication].delegate.window makeToast:@"已完成考试"];
         });
     }];
    
    [self.webViewBridge registerHandler:@"toOnlineExamTest" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *exam = data[@"exam"];
        dispatch_async(dispatch_get_main_queue(), ^(){
            ExamConfirmTestViewController *vc = [[ExamConfirmTestViewController alloc]init];
            [vc setExamJson:exam];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
        [self.jznWebView.scrollView.mj_header beginRefreshing];
}

-(void) requestLearn:(int) page{
    
    NSString *url = [NSString stringWithFormat:@"%@/professionalClass/findClassExamInfo" ,[URLUtil hostName]];
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%ld",self.loginDto.userId],@"professionalClassId":self->_professionalClassId};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            DataResult *examInfo = dataResult.data;
            
//            NSString *url2 = [NSString stringWithFormat:@"%@/classCourse/findOnlineCourseList" ,[URLUtil hostName]];
//            NSDictionary *params2 = @{@"userId":[NSString stringWithFormat:@"%ld",self.loginDto.userId],@"professionalClassId":[[self.examTrain mj_JSONObject] objectForKey:@"professionalClassId"]};
//            [JZNRequestUtil requestUrl:url2 requestParams:params2 success:^(DataResult *dataResult2) {
//                if(dataResult2.code == 0){
//                    NSArray *array = dataResult2.data;
//                    NSString *clear = [NSString stringWithFormat:@"getExamTrain(%@,%@,%@)",[examInfo mj_JSONString],[self.examTrain mj_JSONString],[array mj_JSONString]];
//                    [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
//                        
//                    }];
//                }else {
//                }
//            } failure:^(NSError *error) {
//                
//            }];
            
            NSString *url2 = [NSString stringWithFormat:@"%@/classCourse/findOnlineCourseList" ,[URLUtil hostName]];
            NSDictionary *params2 = @{@"userId":[NSString stringWithFormat:@"%ld",self.loginDto.userId],@"professionalClassId":self->_professionalClassId};
            [JZNRequestUtil requestUrl:url2 requestParams:params2 success:^(DataResult *dataResult2) {
                if(dataResult2.code == 0){
                    if (page == 1) {
                        NSString *clear = @"clear()";
                        //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
                        [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                            
                        }];
                        [self.jznWebView.scrollView.mj_header endRefreshing];
                        [self.jznWebView.scrollView.mj_footer resetNoMoreData];
                    }
                    
                    NSString *clear = [NSString stringWithFormat:@"getExamList(%@,%@)",[dataResult2.data mj_JSONString],[examInfo mj_JSONString]];
                    //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
                    [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                        
                    }];
                    NSArray *array = dataResult.data;
                    if([array count]<10){
                        
                        [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        
                        [self.jznWebView.scrollView.mj_footer endRefreshing];
                    }
                }else {
                }
            } failure:^(NSError *error) {
                
            }];
            
            
           
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
